package com.hitqz.robot.biz.flow.component.agv;

import com.alibaba.fastjson.JSONObject;
import com.hitqz.robot.api.common.entity.Robot;
import com.hitqz.robot.biz.flow.util.LiteFlowUtil;
import com.hitqz.robot.biz.service.RobotService;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.TimeUnit;

/**
 * @author xupkun
 * @date 2025/4/5
 */
@Slf4j
@LiteflowComponent(value = "QueryStoreStatus", name = "[轮对/轮轴]查询缓存架状态")
@AllArgsConstructor
public class QueryStoreStatus extends NodeComponent {

    private final RobotService robotService;

    private final RestTemplate restTemplate;

    @Override
    public void process() throws Exception {
        Robot robot = robotService.getDefaultRobot();
        String uri = "http://127.0.0.1:9999/admin/km/agv/getStoreStatus?name=566&clientId=" +robot.getRobotSn();
        String result = restTemplate.getForObject(uri, String.class);
        log.info("缓存状态查询结果:" + result);
        while (true) {
            LiteFlowUtil.checkInterruptFlag();
            JSONObject jsonObject = JSONObject.parseObject(result);
            if (jsonObject.containsKey("data") && "1".equals(jsonObject.getString("data"))) {
                log.info("缓存架是可以放置的");
                break;
            }
            TimeUnit.SECONDS.sleep(1);
            result = restTemplate.getForObject(uri, String.class);
            log.info("缓存状态查询结果:" + result);
        }
    }
}
